﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Enterprise.Model.Department;
using Enterprise.Service.Department;
using Enterprise.Infrastructure;
using Enterprise.Control;
using Enterprise.Service.User;
namespace Enterprise.UI.Web.Manage.DepartMent
{
    public partial class Edit : Enterprise.Service.BasePage
    {
        int Id = (int)Utility.sink("Id", Utility.MethodType.Get, 0, 0, Utility.DataType.Int);
        string Cmd = (string)Utility.sink("Cmd", Utility.MethodType.Get, 0, 0, Utility.DataType.Str);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                OnStart();
            }
        }

        private void OnStart()
        {
            OnCommand();
            OnBindUser();
            OnBindData();
        }

        private void OnCommand()
        {
            if (Cmd == "Edit")
            {
                CreateBT.AddButton("back.gif", "返回", "Detail.aspx?Id=" + Id, Utility.PopedomType.List, HeadMenu1);
            }
            else
            {
                CreateBT.AddButton("back.gif", "返回", "List.aspx", Utility.PopedomType.List, HeadMenu1);
            }
        }

        private void OnBindUser()
        {
            UserService uService = new UserService();
            var list = uService.UserList();
            tb_Manager.DataSource = list;
            tb_Manager.DataTextField = "UNAME";
            tb_Manager.DataValueField = "USERID";
            tb_Manager.DataBind();
            tb_Manager.Items.Insert(0, new ListItem("请选择...", "0"));

            tb_HeadManager.DataSource = list;
            tb_HeadManager.DataTextField = "UNAME";
            tb_HeadManager.DataValueField = "USERID";
            tb_HeadManager.DataBind();
            tb_HeadManager.Items.Insert(0, new ListItem("请选择...", "0"));
        }

        private void OnBindData()
        {            
            DepartmentService ser = new DepartmentService();
            var q = ser.DepartmentDisp(Id);
            if (q != null)
            {
                if (Cmd == "Edit")
                {
                    tb_Name.Text = q.DNAME;
                    ListItem limanager =tb_Manager.Items.FindByValue(q.DMANAGER.ToString());
                    if (limanager != null)
                        limanager.Selected = true;
                    ListItem liheadermanager =tb_HeadManager.Items.FindByValue(q.DHEADERMANAGER.ToString());
                    if (liheadermanager != null)
                        liheadermanager.Selected = true;                    
                }
                else if (Cmd == "Delete")
                {
                    string _note = "操作成功";
                    q.DB_Option_Action = "Delete";
                    try
                    {
                        ser.DepartmentExecute(q);
                    }
                    catch (Exception ex)
                    {
                        _note = ex.Message;
                    }
                    Utility.ShowMsg(this.Page, "OK", this.Tran(_note), "List.aspx");
                }
            }
        }


        protected void BtnSave_OnClick(object sender, EventArgs e)
        {
            string _note = "操作成功";
            try
            {
                DepartmentService ser = new DepartmentService();
                DepartmentEntity entity = new DepartmentEntity();
                if (string.IsNullOrEmpty(Cmd))
                {
                    entity.DB_Option_Action = "Insert";
                    entity.DORDERBY = ser.GetMaxDepartmentOrderBy(Id) + 1;
                }
                else if (Cmd == "Edit")
                {                    
                    entity = ser.DepartmentDisp(Id);
                    entity.DB_Option_Action = "Update";
                }
                entity.DNAME = (string)Utility.sink(tb_Name.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
                entity.DMANAGER = (int)Utility.sink(tb_Manager.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Int);
                entity.DHEADERMANAGER = (int)Utility.sink(tb_HeadManager.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Int);
                ser.DepartmentExecute(entity);
            }
            catch (Exception ex)
            {
                _note = ex.Message;
            }
             Utility.ShowMsg(this.Page, "OK", this.Tran(_note), "List.aspx");
        }

    }
}